# koroFileHeader at Yungoal acer
# Create: 2022-02-21 18:16:55
# LastEdit: 2022-03-07 16:22:33
"""说明暂无"""
__author__ = '749B'

from django.contrib import admin
from django.db import models

# Create your models here.
class BluekingJobs(models.Model):
    name = models.CharField(max_length=128, unique=True, verbose_name="名称")
    purpose = models.CharField(max_length=512, verbose_name="通途")
    user = models.CharField(max_length=128, blank=True, verbose_name="使用人")
    deploy_time = models.DateField(verbose_name='部署日期')
    description = models.TextField(blank=True, verbose_name='描述')
    dev_project_choices = [
        (1, 'Azure Client'),
        (2, 'Jupyter'),
        (0, '其他'),
    ]
    dev_project = models.SmallIntegerField(verbose_name='开发项目', choices=dev_project_choices)
    dev_filename = models.CharField(max_length=128, verbose_name="文件名")

    class Meta:
        verbose_name = "蓝鲸作业"
        verbose_name_plural = "蓝鲸作业列表"

    def __str__(self) -> str:
        return self.name


class TaskList(models.Model):
    name = models.CharField(max_length=128, verbose_name="任务名称")
    customer = models.CharField(max_length=128, verbose_name="客户名称")
    task_typ = models.CharField(max_length=128, verbose_name='作业类别', blank=True)
    staff = models.CharField(max_length=128, verbose_name='作业人员', blank=True)
    working_hours = models.DurationField(verbose_name='当前需要工时', blank=True, null=True, help_text='半天: P0.5D')
    description = models.TextField(verbose_name='作业内容描述', blank=True)
    uptime = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "固定作业"
        verbose_name_plural = "固定作业自动化调查表"

    # 添加auto_now的字段, 在通过本作业表保存关联的作业进展记录的时候, uptime会更新
    # 间接实现了按最近一次进展的时间排序的功能, admin.py 里有直接通过聚合关联的作业进展的时间实现排序的方式
    # 更改本作业表的其他字段内容也会导致该条记录排到最前, 功能保留但是admin中使用新的更准确的实现方式
    @admin.display(description='最近更新', ordering='-uptime')
    def latest_task(self):
        last_taskprogress = self.taskprogress_set.last()
        if last_taskprogress:
            return '{:%y-%m-%d} {}'.format(last_taskprogress.ctime, last_taskprogress.progress)

    def __str__(self) -> str:
        return self.name

class TaskProgress(models.Model):
    task = models.ForeignKey(TaskList, on_delete=models.CASCADE)
    progress = models.CharField(max_length=128, verbose_name="进展")
    description = models.TextField(verbose_name='详情', blank=True)
    ctime = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = "固定作业进展"
        verbose_name_plural = "固定作业进展表"

    def __str__(self) -> str:
        return '{:%y-%m-%d %H:%M:%S}'.format(self.ctime)
